package com.yunhe.policyfulfillment.policyful.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yunhe.common.exception.ArgumentErrorException;
import com.yunhe.common.model.mybatis.PageResult;
import com.yunhe.policyfulfillment.constant.PolicyFulfillmentConstant;
import com.yunhe.policyfulfillment.policyful.domain.PolicyInfoNews;
import com.yunhe.policyfulfillment.policyful.model.NewStauts;
import com.yunhe.policyfulfillment.policyful.model.PolicyInfoNewsQueryCondition;
import com.yunhe.policyfulfillment.policyful.model.PortalNewQueryCondition;
import com.yunhe.policyfulfillment.policyful.model.PublishStatus;
import com.yunhe.policyfulfillment.policyful.repository.PolicyInfoNewsRepository;
import com.yunhe.policyfulfillment.policyful.service.PolicyInfoNewsService;
import com.yunhe.policyfulfillment.util.MyHeaderUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yunhe.common.util.CollectionUtil;

import java.util.Date;
import java.util.List;

/**
 * @ClassName PolicyInfoNewsServiceImpl
 * @Description TODO
 * @Author HeTao
 * @Date 2021/7/23 16:49
 * @Version 1.0
 **/
@Service
public class PolicyInfoNewsServiceImpl implements PolicyInfoNewsService {

    @Autowired
    private PolicyInfoNewsRepository policyInfoNewsRepository;

    @Autowired
    private MyHeaderUtil myHeaderUtil;

    @Override
    public int createPolicyInfoNews(PolicyInfoNews policyInfoNews) throws ArgumentErrorException {
        Long userId = myHeaderUtil.getHeaderUserId();
        if(userId ==null){
            throw new ArgumentErrorException("登录信息已失效,请重新登录");
        }
        policyInfoNews.setPublishPerson(userId);
        if(policyInfoNews.getPublishTime().getTime() <= System.currentTimeMillis()){
            policyInfoNews.setIsOnline(PolicyFulfillmentConstant.Online_Type.Online.value());
        }
        return policyInfoNewsRepository.createPolicyInfoNewsSelective(policyInfoNews);
    }

    @Override
    public int deletePolicyInfoNewsById(long id) {
        return policyInfoNewsRepository.deletePolicyInfoNewsByPrimaryKey(id);
    }

    @Override
    public int updatePolicyInfoNews(PolicyInfoNews policyInfoNews) {
        if (myHeaderUtil.getHeaderUser() != null) {
            policyInfoNews.setUpdatePerson(myHeaderUtil.getHeaderUserId());
        }
        long time = policyInfoNews.getPublishTime().getTime();
        if (time <= System.currentTimeMillis()){
            policyInfoNews.setIsOnline(PolicyFulfillmentConstant.Online_Type.Online.value());
        }else {
            policyInfoNews.setIsOnline(PolicyFulfillmentConstant.Online_Type.Offline.value());
        }
        return policyInfoNewsRepository.updatePolicyInfoNewsByPrimaryKeySelective(policyInfoNews);
    }

    @Override
    public PolicyInfoNews findPolicyInfoNewsById(long id) {
        PolicyInfoNews policyInfoNews = policyInfoNewsRepository.searchPolicyInfoNewsByPrimaryKey(id);
        if (policyInfoNews != null) {
            policyInfoNews.setPublishPersonName(myHeaderUtil.getUserName(policyInfoNews.getPublishPerson()));
            if (policyInfoNews.getUpdatePerson() != null){
                policyInfoNews.setUpdatePersonName(myHeaderUtil.getUserName(policyInfoNews.getUpdatePerson()));
            }
        }
        return policyInfoNews;
    }

    @Override
    public PageResult findPolicyInfoNewsByQueryCondition(PolicyInfoNewsQueryCondition policyInfoNewsQueryCondition) throws ArgumentErrorException {
        PageResult result = null;
        Long userId = myHeaderUtil.getHeaderUserId();
        if(userId ==null){
            throw new ArgumentErrorException("登录信息已失效,请重新登录!");
        }
        /*if(policyInfoNewsQueryCondition.getPublishPerson() == null){
            policyInfoNewsQueryCondition.setPublishPerson(userId);
        }*/
        PageHelper.startPage(policyInfoNewsQueryCondition.getPage(),policyInfoNewsQueryCondition.getSize());
        List<PolicyInfoNews> policyInfoNews =  policyInfoNewsRepository.findPolicyInfoNewsByQueryCondition(policyInfoNewsQueryCondition);
        for (PolicyInfoNews policyInfoNew : policyInfoNews) {
            policyInfoNew.setPublishPersonName(myHeaderUtil.getUserName(policyInfoNew.getPublishPerson()));
            if (policyInfoNew.getUpdatePerson() != null){
                policyInfoNew.setUpdatePersonName(myHeaderUtil.getUserName(policyInfoNew.getUpdatePerson()));
            }
        }
        PageInfo<PolicyInfoNews> pageInfo = new PageInfo<>(policyInfoNews);
        result = new PageResult(policyInfoNewsQueryCondition.getPage(),policyInfoNewsQueryCondition.getSize(), (int) pageInfo.getTotal(),policyInfoNews);
        return result;
    }

    @Override
    public void setTop(long id) {
        PolicyInfoNews policyInfoNews = new PolicyInfoNews();
        policyInfoNews.setId(id);
        policyInfoNews.setUpdateTime(new Date());
        policyInfoNews.setIsTop(PolicyFulfillmentConstant.Top_Type.Top.value());
        policyInfoNews.setIsOnline(PolicyFulfillmentConstant.Online_Type.Online.value());
        policyInfoNews.setPublishTime(new Date());
        if (myHeaderUtil.getHeaderUser() != null){
            policyInfoNews.setUpdatePerson(myHeaderUtil.getHeaderUserId());
        }
        policyInfoNewsRepository.updatePolicyInfoNewsByPrimaryKeySelective(policyInfoNews);
    }

    @Override
    public void setHot(NewStauts newStauts) throws ArgumentErrorException {

        PolicyInfoNews policyInfoNews = new PolicyInfoNews();
        policyInfoNews.setId(newStauts.getId());
        policyInfoNews.setUpdateTime(new Date());
        policyInfoNews.setIsHot(newStauts.getStatus());
        if(PolicyFulfillmentConstant.Hot_Type.Hot.value().equals(newStauts.getStatus())){
            //判断当前热门条数是否超过五条
            int hotCount = policyInfoNewsRepository.getCountByIsHot();
            if(hotCount >=5){
                throw new ArgumentErrorException("热门新闻限制设置最多五条!");
            }
            policyInfoNews.setPublishTime(new Date());
            policyInfoNews.setIsOnline(PolicyFulfillmentConstant.Online_Type.Online.value());
        }
        if (myHeaderUtil.getHeaderUser() != null){
            policyInfoNews.setUpdatePerson(myHeaderUtil.getHeaderUserId());
        }
        policyInfoNewsRepository.updatePolicyInfoNewsByPrimaryKeySelective(policyInfoNews);
    }

    @Override
    public void setNew(NewStauts newStauts) throws ArgumentErrorException {
        PolicyInfoNews policyInfoNews = new PolicyInfoNews();
        policyInfoNews.setId(newStauts.getId());
        policyInfoNews.setUpdateTime(new Date());
        policyInfoNews.setIsNew(newStauts.getStatus());
        if(PolicyFulfillmentConstant.New_Type.New.value().equals(newStauts.getStatus())){
            int newCount = policyInfoNewsRepository.getCountByIsNew();
            if(newCount >=5){
                throw new ArgumentErrorException("最新新闻限制设置最多五条!");
            }
            policyInfoNews.setPublishTime(new Date());
            policyInfoNews.setIsOnline(PolicyFulfillmentConstant.Online_Type.Online.value());
        }
        if (myHeaderUtil.getHeaderUser() != null){
            policyInfoNews.setUpdatePerson(myHeaderUtil.getHeaderUserId());
        }
        policyInfoNewsRepository.updatePolicyInfoNewsByPrimaryKeySelective(policyInfoNews);
    }

    @Override
    public void setPublishStatus(PublishStatus publishStatus) {
        List<Long> ids = publishStatus.getIds();
        if(!CollectionUtil.isEmpty(ids)){
            for (Long id : ids) {
                PolicyInfoNews policyInfoNews = new PolicyInfoNews();
                policyInfoNews.setId(id);
                //policyInfoNews.setUpdateTime(new Date());
                //policyInfoNews.setIsNew(PolicyFulfillmentConstant.New_Type.New.value());
                policyInfoNews.setIsOnline(publishStatus.getPublishStatus());
                if(PolicyFulfillmentConstant.Online_Type.Online.value().equals(publishStatus.getPublishStatus())){
                    policyInfoNews.setPublishTime(new Date());
                }else {
                    policyInfoNews.setIsHot(PolicyFulfillmentConstant.Hot_Type.NotHot.value());
                    policyInfoNews.setIsNew(PolicyFulfillmentConstant.New_Type.NotNew.value());
                }
                if (myHeaderUtil.getHeaderUser() != null){
                    policyInfoNews.setUpdatePerson(myHeaderUtil.getHeaderUserId());
                }
                policyInfoNewsRepository.updatePolicyInfoNewsByPrimaryKeySelective(policyInfoNews);
            }
        }
    }

    @Override
    public List<PolicyInfoNews> findHotNew() {
        return policyInfoNewsRepository.findHotNew();
    }

    @Override
    public List<PolicyInfoNews> findNewByTitle(String title) {
        return policyInfoNewsRepository.findNewByTitle(title);
    }

    @Override
    public PageResult findNewByCondition(PortalNewQueryCondition portalNewQueryCondition) {
        PageResult result = null;
        PageHelper.startPage(portalNewQueryCondition.getPage(),portalNewQueryCondition.getSize());
        PolicyInfoNewsQueryCondition policyInfoNewsQueryCondition = new PolicyInfoNewsQueryCondition();
        BeanUtils.copyProperties(portalNewQueryCondition,policyInfoNewsQueryCondition);
        policyInfoNewsQueryCondition.setIsOnline(PolicyFulfillmentConstant.Online_Type.Online.value());
        List<PolicyInfoNews> policyInfoNews =  policyInfoNewsRepository.findPortalPolicyInfoNewsByQueryCondition(policyInfoNewsQueryCondition);
        for (PolicyInfoNews policyInfoNew : policyInfoNews) {
            if (policyInfoNew.getUpdatePerson() != null){
                policyInfoNew.setUpdatePersonName(myHeaderUtil.getUserName(policyInfoNew.getUpdatePerson()));
            }
        }
        PageInfo<PolicyInfoNews> pageInfo = new PageInfo<>(policyInfoNews);
        result = new PageResult(policyInfoNewsQueryCondition.getPage(),policyInfoNewsQueryCondition.getSize(), (int) pageInfo.getTotal(),policyInfoNews);
        return result;
    }

    @Override
    public void publishTimedNew() {
        policyInfoNewsRepository.publishTimedNew();
    }
}
